package memory

/*创建一个简单的内存定位器作为备选方案*/
import (
	"gameServer/locate"
	"gameServer/log"
	. "gameServer/registry"
)

// MemoryLocator 内存服务定位器
type MemoryLocator struct {
	balancer locate.Balancer
}

// NewMemoryLocator 创建内存服务定位器
func NewMemoryLocator() *MemoryLocator {
	return &MemoryLocator{
		balancer: locate.NewRoundRobinBalancer(),
	}
}

// WithBalancer 设置负载均衡算法
func (l *MemoryLocator) WithBalancer(balancer locate.Balancer) *MemoryLocator {
	l.balancer = balancer
	return l
}

// Select 选择服务节点
func (l *MemoryLocator) Select(serviceName string, route uint32) (*ServiceInfo, error) {
	return l.balancer.Select(serviceName, route)
}

// UpdateServices 更新服务列表
func (l *MemoryLocator) UpdateServices(services []*ServiceInfo) error {
	l.balancer.Update(services)
	log.Infof("Services updated in memory locator: %d services", len(services))
	return nil
}

// Start 启动定位器（内存定位器无需特殊启动）
func (l *MemoryLocator) Start() error {
	log.Info("Memory locator started")
	return nil
}

// Stop 停止定位器（内存定位器无需特殊停止）
func (l *MemoryLocator) Stop() error {
	log.Info("Memory locator stopped")
	return nil
}
